package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.config.AliyunOSSUtils;
import com.ruoyi.system.domain.Advertising;
import com.ruoyi.system.service.IAdvertisingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 广告设置Controller
 * 
 * @author ruoyi
 * @date 2024-10-23
 */
@RestController
@RequestMapping("/advertising")
public class AdvertisingController extends BaseController
{
    @Autowired
    private IAdvertisingService advertisingService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;
    /**
     * 查询广告设置列表
     */
    @RequiresPermissions("advertising:advertising:list")
    @GetMapping("/list")
    public TableDataInfo list(Advertising advertising)
    {
        startPage();
        List<Advertising> list = advertisingService.selectAdvertisingList(advertising);
        return getDataTable(list);
    }

    /**
     * 导出广告设置列表
     */
    @RequiresPermissions("advertising:advertising:export")
    @Log(title = "广告设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Advertising advertising)
    {
        List<Advertising> list = advertisingService.selectAdvertisingList(advertising);
        ExcelUtil<Advertising> util = new ExcelUtil<Advertising>(Advertising.class);
        util.exportExcel(response, list, "广告设置数据");
    }

    /**
     * 获取广告设置详细信息
     */
    @RequiresPermissions("advertising:advertising:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(advertisingService.selectAdvertisingById(id));
    }

    /**
     * 新增广告设置
     */
    @RequiresPermissions("advertising:advertising:add")
    @Log(title = "广告设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Advertising advertising)
    {
        return toAjax(advertisingService.insertAdvertising(advertising));
    }

    /**
     * 修改广告设置
     */
    @RequiresPermissions("advertising:advertising:edit")
    @Log(title = "广告设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Advertising advertising)
    {
        return toAjax(advertisingService.updateAdvertising(advertising));
    }

    /**
     * 删除广告设置
     */
    @RequiresPermissions("advertising:advertising:remove")
    @Log(title = "广告设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(advertisingService.deleteAdvertisingByIds(ids));
    }

    //图片上传下载
    @RequestMapping("fileUpload")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();
        String s = aliyunOSSUtils.putObject(inputStream, originalFilename);
        return s;
    }
}
